// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   MessagesConfigurator.java

package com.boci.common.util;

import com.boci.common.constant.ClientAppConst;
import java.io.*;
import java.util.*;

// Referenced classes of package com.boci.common.util:
//            FileUtil

public class MessagesConfigurator
{

    public static MessagesConfigurator getInstance()
        throws UnsupportedEncodingException, FileNotFoundException, IOException
    {
        if(instance == null)
            instance = new MessagesConfigurator();
        return instance;
    }

    private MessagesConfigurator()
        throws UnsupportedEncodingException, FileNotFoundException, IOException
    {
        uiResourcesEngHashMap = new HashMap();
        uiResourcesTradChineseHashMap = new HashMap();
        uiResourcesSimChineseHashMap = new HashMap();
        errorEngHashMap = new HashMap();
        errorTradChineseHashMap = new HashMap();
        errorSimChineseHashMap = new HashMap();
        initHashMap("com/boci/common/resources/config/ui_resources", uiResourcesEngHashMap, ClientAppConst.EN_US);
        initHashMap("com/boci/common/resources/config/ui_resources", uiResourcesTradChineseHashMap, ClientAppConst.ZH_HK);
        initHashMap("com/boci/common/resources/config/ui_resources", uiResourcesSimChineseHashMap, ClientAppConst.ZH_CN);
        initHashMap("com/boci/common/resources/config/error", errorEngHashMap, ClientAppConst.EN_US);
        initHashMap("com/boci/common/resources/config/error", errorTradChineseHashMap, ClientAppConst.ZH_HK);
        initHashMap("com/boci/common/resources/config/error", errorSimChineseHashMap, ClientAppConst.ZH_CN);
        strPageRegex = new String("%n".getBytes("ISO-8859-1"), "UTF-8");
        strTotalPageRegex = new String("%t".getBytes("ISO-8859-1"), "UTF-8");
        initStaffOptionDesc();
        initCommonQuestions();
    }

    private void initStaffOptionDesc()
        throws FileNotFoundException, IOException
    {
        strEngStaffOptionDesc = readTextFile("/com/boci/common/resources/config/staff_option_en_US.txt");
        strChiStaffOptionDesc = readTextFile("/com/boci/common/resources/config/staff_option_zh_HK.txt");
        strChsStaffOptionDesc = readTextFile("/com/boci/common/resources/config/staff_option_zh_CN.txt");
    }

    private void initCommonQuestions()
        throws FileNotFoundException, IOException
    {
        strEngCommonQuestions = readTextFile("/com/boci/common/resources/config/question_en_US.txt");
        strChiCommonQuestions = readTextFile("/com/boci/common/resources/config/question_zh_HK.txt");
        strChsCommonQuestions = readTextFile("/com/boci/common/resources/config/question_zh_CN.txt");
    }

    private String readTextFile(String pFileLocation)
        throws FileNotFoundException, IOException
    {
        InputStream ins = getClass().getResourceAsStream(pFileLocation);
        if(ins == null)
            return "";
        BufferedReader reader = FileUtil.getReader(ins, "UTF-8");
        StringBuffer sbContents = new StringBuffer("");
        String strLine;
        while((strLine = reader.readLine()) != null) 
        {
            sbContents.append(strLine);
            sbContents.append(System.getProperty("line.separator"));
        }
        reader.close();
        ins.close();
        return sbContents.toString();
    }

    public String getPageRegex()
    {
        return strPageRegex;
    }

    public String getTotalPageRegex()
    {
        return strTotalPageRegex;
    }

    private void initHashMap(String pFileBase, HashMap pDataHashMap, Locale pLocale)
        throws UnsupportedEncodingException
    {
        ResourceBundle rb = ResourceBundle.getBundle(pFileBase, pLocale);
        String strKey;
        String strValue;
        for(Enumeration oEnum = rb.getKeys(); oEnum.hasMoreElements(); pDataHashMap.put(strKey, strValue))
        {
            strKey = (String)oEnum.nextElement();
            strValue = rb.getString(strKey);
            strValue = new String(strValue.getBytes("ISO-8859-1"), "UTF-8");
        }

    }

    public String getUIMessages(String pKey, Locale pLocale)
    {
        String strRtn = "";
        String strTmpKey = pKey.replaceAll(" ", "_");
        strTmpKey = strTmpKey.toUpperCase();
        if(pLocale.equals(ClientAppConst.ZH_HK))
        {
            if(uiResourcesTradChineseHashMap == null || uiResourcesTradChineseHashMap.isEmpty())
                return null;
            strRtn = (String)uiResourcesTradChineseHashMap.get(strTmpKey);
        } else
        if(pLocale.equals(ClientAppConst.ZH_CN))
        {
            if(uiResourcesSimChineseHashMap == null || uiResourcesSimChineseHashMap.isEmpty())
                return null;
            strRtn = (String)uiResourcesSimChineseHashMap.get(strTmpKey);
        } else
        {
            if(uiResourcesEngHashMap == null || uiResourcesEngHashMap.isEmpty())
                return null;
            strRtn = (String)uiResourcesEngHashMap.get(strTmpKey);
        }
        if(strRtn == null)
            strRtn = pKey;
        else
            strRtn = strRtn.trim();
        return strRtn;
    }

    public String[] getMsgArray(String pPrefix, String pKeys[], Locale pLocale)
    {
        String strMsgs[] = new String[pKeys.length];
        for(int i = 0; i < pKeys.length; i++)
        {
            String key = (new StringBuilder()).append(pPrefix).append(pKeys[i].trim()).toString();
            strMsgs[i] = getUIMessages(key, pLocale);
        }

        return strMsgs;
    }

    public String getErrorMessage(String pKey, Locale pLocale)
    {
        String strRtn = "";
        if(pLocale.equals(ClientAppConst.ZH_HK))
        {
            if(errorTradChineseHashMap == null || errorTradChineseHashMap.isEmpty())
                return pKey;
            strRtn = (String)errorTradChineseHashMap.get(pKey);
        } else
        if(pLocale.equals(ClientAppConst.ZH_CN))
        {
            if(errorSimChineseHashMap == null || errorSimChineseHashMap.isEmpty())
                return pKey;
            strRtn = (String)errorSimChineseHashMap.get(pKey);
        } else
        {
            if(errorEngHashMap == null || errorEngHashMap.isEmpty())
                return pKey;
            strRtn = (String)errorEngHashMap.get(pKey);
        }
        if(strRtn == null || strRtn.length() == 0)
            return pKey;
        else
            return strRtn;
    }

    public String getStaffOptionDesc(Locale pLocale)
    {
        if(pLocale.equals(ClientAppConst.EN_US))
            return strEngStaffOptionDesc;
        if(pLocale.equals(ClientAppConst.ZH_HK))
            return strChiStaffOptionDesc;
        else
            return strChsStaffOptionDesc;
    }

    public String getCommonQuestions(Locale pLocale)
    {
        if(pLocale.equals(ClientAppConst.EN_US))
            return strEngCommonQuestions;
        if(pLocale.equals(ClientAppConst.ZH_HK))
            return strChiCommonQuestions;
        else
            return strChsCommonQuestions;
    }

    private HashMap uiResourcesEngHashMap;
    private HashMap uiResourcesTradChineseHashMap;
    private HashMap uiResourcesSimChineseHashMap;
    private HashMap errorEngHashMap;
    private HashMap errorTradChineseHashMap;
    private HashMap errorSimChineseHashMap;
    private static MessagesConfigurator instance;
    private String strPageRegex;
    private String strTotalPageRegex;
    private String strEngStaffOptionDesc;
    private String strChiStaffOptionDesc;
    private String strChsStaffOptionDesc;
    private String strEngCommonQuestions;
    private String strChiCommonQuestions;
    private String strChsCommonQuestions;
}
